package com.ruoyi.livesass;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruoyi.vo.QuizStatisticsVo;
import com.volcengine.model.beans.livesaas.Coupon;
import com.volcengine.model.beans.livesaas.CreateActivityCouponModel;
import com.volcengine.model.beans.livesaas.CreateCouponModel;
import com.volcengine.model.beans.livesaas.MenuMsgNew;
import com.volcengine.model.beans.livesaas.ProductAPIs;
import com.volcengine.model.beans.livesaas.ProductMsgAPI;
import com.volcengine.model.livesaas.request.CommonRequest;
import com.volcengine.model.livesaas.request.CreateActivityCouponsRequest;
import com.volcengine.model.livesaas.request.CreateCouponsRequest;
import com.volcengine.model.livesaas.request.DeleteActivityCouponsRequest;
import com.volcengine.model.livesaas.request.DeleteCouponsRequest;
import com.volcengine.model.livesaas.request.ExplainProductRequest;
import com.volcengine.model.livesaas.request.GetActivityAllCouponsPickDataRequest;
import com.volcengine.model.livesaas.request.GetActivityCouponPickDataRequest;
import com.volcengine.model.livesaas.request.ListCouponsRequest;
import com.volcengine.model.livesaas.request.ListProductCardsRequest;
import com.volcengine.model.livesaas.request.ListQuizRecordAPIRequest;
import com.volcengine.model.livesaas.request.ListQuizStatisticsAPIRequest;
import com.volcengine.model.livesaas.request.ModifyActivityMenusRequest;
import com.volcengine.model.livesaas.request.SendActivityCouponRequest;
import com.volcengine.model.livesaas.request.SendProductOrderMessageRequest;
import com.volcengine.model.livesaas.request.StopActivityCouponRequest;
import com.volcengine.model.livesaas.request.UpdateActivityCouponRequest;
import com.volcengine.model.livesaas.request.UpdateActivityProductV2Request;
import com.volcengine.model.livesaas.request.UpdateCouponRequest;
import com.volcengine.model.livesaas.response.CommonStatusResponse;
import com.volcengine.model.livesaas.response.CreateActivityCouponsResponse;
import com.volcengine.model.livesaas.response.CreateActivityCouponsResponse.CreateActivityCouponsResponseBody;
import com.volcengine.model.livesaas.response.CreateCouponsResponse;
import com.volcengine.model.livesaas.response.CreateCouponsResponse.CreateCouponsResponseBody;
import com.volcengine.model.livesaas.response.DeleteActivityCouponsResponse;
import com.volcengine.model.livesaas.response.DeleteActivityCouponsResponse.DeleteActivityCouponsResponseBody;
import com.volcengine.model.livesaas.response.DeleteCouponsResponse;
import com.volcengine.model.livesaas.response.DeleteCouponsResponse.DeleteCouponsResponseBody;
import com.volcengine.model.livesaas.response.GetActivityAllCouponsPickDataResponse;
import com.volcengine.model.livesaas.response.GetActivityAllCouponsPickDataResponse.GetActivityAllCouponsPickDataResponseBody;
import com.volcengine.model.livesaas.response.GetActivityCouponPickDataResponse;
import com.volcengine.model.livesaas.response.GetActivityCouponPickDataResponse.GetActivityCouponPickDataResponseBody;
import com.volcengine.model.livesaas.response.GetActivityProductAPIResponse;
import com.volcengine.model.livesaas.response.ListCouponsResponse;
import com.volcengine.model.livesaas.response.ListProductCardsResponse;
import com.volcengine.model.livesaas.response.ListQuizRecordAPIResponse;
import com.volcengine.model.livesaas.response.ListQuizRecordAPIResponse.ListQuizRecordAPIResponseBody;
import com.volcengine.model.livesaas.response.ListQuizRecordAPIResponse.QuizRecordAPI;
import com.volcengine.model.livesaas.response.ListQuizStatisticsAPIResponse;
import com.volcengine.model.livesaas.response.ListQuizStatisticsAPIResponse.QuizStatisticsAPI;
import com.volcengine.model.livesaas.response.ModifyActivityMenusResponse;
import com.volcengine.model.livesaas.response.SendActivityCouponResponse;
import com.volcengine.model.livesaas.response.SendActivityCouponResponse.SendActivityCouponResponseBody;
import com.volcengine.model.livesaas.response.SendProductOrderMessageResponse;
import com.volcengine.model.livesaas.response.StopActivityCouponResponse;
import com.volcengine.model.livesaas.response.StopActivityCouponResponse.StopActivityCouponResponseBody;
import com.volcengine.model.livesaas.response.UpdateActivityCouponResponse;
import com.volcengine.model.livesaas.response.UpdateActivityCouponResponse.UpdateActivityCouponResponseBody;
import com.volcengine.model.livesaas.response.UpdateCouponResponse;
import com.volcengine.model.livesaas.response.UpdateCouponResponse.UpdateCouponResponseBody;
import com.volcengine.service.livesaas.LivesaasService;
import com.volcengine.service.livesaas.impl.LivesaasServiceImpl;
/**
 * 答题相关接口
*/
@Component
@Order(2)
public class QuizRecordService implements ApplicationRunner {
	
	
	private static final Logger LOG = LoggerFactory.getLogger(QuizRecordService.class);

	LivesaasService livesaasService;
	
	@Override
	public void run(ApplicationArguments args) throws Exception {
		livesaasService = LivesaasServiceImpl.getInstance();
		
		
	}
	
	/**
	 * 获取直播间答题信息
	 * @param zbjlid
	 * @return
	 * @throws Exception
	 */
	public List<QuizStatisticsAPI> listQuizStatisticsAPI(Long zbjlid){
		ListQuizStatisticsAPIRequest listQuizStatisticsAPIRequest = new ListQuizStatisticsAPIRequest();
		listQuizStatisticsAPIRequest.setActivityId(zbjlid);
		listQuizStatisticsAPIRequest.setPageNo(1);
		listQuizStatisticsAPIRequest.setPageItemCount(500);
		ListQuizStatisticsAPIResponse listQuizStatisticsAPIResponse;
		try {
			listQuizStatisticsAPIResponse = livesaasService.listQuizStatisticsAPI(listQuizStatisticsAPIRequest);
			return listQuizStatisticsAPIResponse.getResult().getQuizStatisticsList();
		} catch (Exception e) {
			e.printStackTrace();
	        throw new RuntimeException("操作失败，请重试"+e.getMessage());

		}
	}
	

	/**
	 * 获取直播间用户的答题信息
	 * @param zbjlid
	 * @param quizID
	 * @param pageNo
	 * @return
	 * @throws Exception
	 */
	public List<QuizStatisticsVo> listQuizRecordAPI(Long zbjlid,Long quizid,int pageNo){
		ListQuizRecordAPIRequest listQuizRecordAPIRequest = new ListQuizRecordAPIRequest();
		listQuizRecordAPIRequest.setActivityId(zbjlid);
		listQuizRecordAPIRequest.setQuizID(quizid);
		listQuizRecordAPIRequest.setPageNo(pageNo);
		listQuizRecordAPIRequest.setPageItemCount(500);
		ListQuizRecordAPIResponse listQuizRecordAPIResponse;
		try {
			listQuizRecordAPIResponse = livesaasService.listQuizRecordAPI(listQuizRecordAPIRequest);
			List<QuizRecordAPI> list = listQuizRecordAPIResponse.getResult().getQuizStatisticsList();
			return dos2vos(list, zbjlid, quizid);
		} catch (Exception e) {
			e.printStackTrace();
	        throw new RuntimeException("操作失败，请重试"+e.getMessage());

		}
	
	}
	
	
	/**
	 * 获取总条数
	 * @param zbjlid
	 * @param quizID
	 * @param pageNo
	 * @return
	 * @throws Exception
	 */
	public Integer getPageTotalCount(Long zbjlid,Long quizid){
		ListQuizRecordAPIRequest listQuizRecordAPIRequest = new ListQuizRecordAPIRequest();
		listQuizRecordAPIRequest.setActivityId(zbjlid);
		listQuizRecordAPIRequest.setQuizID(quizid);
		listQuizRecordAPIRequest.setPageNo(1);
		listQuizRecordAPIRequest.setPageItemCount(1);
		ListQuizRecordAPIResponse listQuizRecordAPIResponse;
		try {
			listQuizRecordAPIResponse = livesaasService.listQuizRecordAPI(listQuizRecordAPIRequest);
			return listQuizRecordAPIResponse.getResult().getPageTotalCount();
		} catch (Exception e) {
			e.printStackTrace();
	        throw new RuntimeException("操作失败，请重试"+e.getMessage());
		}
	}
	
	
	/**
	 * 转换
	 * @param list
	 * @param zbjlid
	 * @param quizid
	 * @return
	 */
	private List<QuizStatisticsVo> dos2vos(List<QuizRecordAPI> list,Long zbjlid,Long quizid){
		List<QuizStatisticsVo> retList = new ArrayList<QuizStatisticsVo>();
		if(list!=null) {
			for (QuizRecordAPI quizRecordAPI : list) {
				retList.add(new QuizStatisticsVo(zbjlid,quizid,quizRecordAPI));
			}
		}
		return retList;
	}
 
	
	
	
	
}
